package cn.slipi.common.rule.custom.entity;

import cn.slipi.common.constants.Constants;
import com.alibaba.fastjson.annotation.JSONType;
import lombok.*;
import org.springframework.http.server.reactive.ServerHttpRequest;

import java.io.Serializable;
import java.util.Optional;

/**
 * MatchCondition
 */
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@JSONType(orders = {"ip", "appKey", "apiCode", "rpcType", "signCode", "queryType"})
public class MatchCondition implements Serializable {

    private String ip;

    private String appKey;

    private String apiCode;

    private String rpcType;

    private String signCode;

    private String queryType;

    public static MatchCondition transform(final ServerHttpRequest request) {
        final String ip = Optional.ofNullable(request.getHeaders().getFirst(Constants.IP)).orElse("");
        final String appKey = Optional.ofNullable(request.getHeaders().getFirst(Constants.APP_KEY)).orElse("");
        final String apiCode = Optional.ofNullable(request.getHeaders().getFirst(Constants.API_CODE)).orElse("");
        final String rpcType = Optional.ofNullable(request.getHeaders().getFirst(Constants.RPC_TYPE)).orElse("");
        final String signCode = Optional.ofNullable(request.getHeaders().getFirst(Constants.SIGN_CODE)).orElse("");
        final String queryType = Optional.ofNullable(request.getHeaders().getFirst(Constants.QUERY_TYPE)).orElse("");
        return new MatchCondition(ip, appKey, apiCode, rpcType, signCode, queryType);
    }

}
